// 这个文件专门存放关于收藏视频的所有接口

const collect=require('koa-router')()
const db=require('../util/db')
// const bodyparser = require('koa-bodyparser')
// collect.use(bodyparser())//调用这个中间件 就可以拿到前端post过来的数据


// http://api.lzjlw.top:3456/collect/collections?videoId=${videoId} && userId=${app.userId}
collect.get('/collections',async (ctx)=>{
    // ctx.query可以获取对象形式的？拼接的内容
    var obj=ctx.query
    console.log(obj)
    // var selectSql=`select * from collections where videoId=${obj.videoId} and userId=${obj.userId}`
    var str = 'where '
    for (key in obj) {
        var name = `${key}='${obj[key]}' and `
        str += name
    }
    let selectSql = 'select * from collections '+str.slice(0,-4)

    let res=await new Promise((resolve,reject)=>{
        db.query(selectSql,(err,result)=>{
            if(err){
                throw err
            }else{
                resolve(result)
            }
        })
    })
    ctx.body=res[0]
})


// http://api.lzjlw.top:3030/collect/collections
collect.post('/collections',async (ctx)=>{
    console.log(ctx)
    var obj=ctx.request.body
    let insertSql=`insert into collections(userId,videoId) values(${ctx.request.body.userId},${ctx.request.body.videoId})`
    let res=await new Promise((resolve,reject)=>{
        db.query(insertSql,(err,result)=>{
            if(err){
                throw err
            }
            else{
                resolve('add success')
            }
        })
    })
    ctx.body=res
})

// http://api.lzjlw.top:3030/collect/collections?videoId=${this.videoId} && userId=${app.userId}
collect.delete('/collections',async (ctx)=>{
    var obj=ctx.query
    console.log(obj)
    let deleteSql=`delete from collections where videoId=${obj.videoId} and userId=${obj.userId}`
    let res=await new Promise((resolve,reject)=>{
        db.query(deleteSql,(err,result)=>{
            if(err){
                throw err
            }
            else{
                resolve('delete success')
            }
        })
    })
    ctx.body=res

})

module.exports=collect

// http://api.lzjlw.top:3456/collect/collections?videoId=${videoId} && userId=${app.userId}
collect.get('/collections',async (ctx)=>{
    // ctx.query可以获取对象形式的？拼接的内容
    var obj=ctx.query
    console.log(obj)
    var selectSql=`select * from collections where videoId=${obj.videoId} and userId=${obj.userId}`
    let res=await new Promise((resolve,reject)=>{
        db.query(selectSql,(err,result)=>{
            if(err){
                throw err
            }else{
                resolve(result)
            }
        })
    })
    ctx.body=res[0]
})


// http://api.lzjlw.top:3030/collect/collections
collect.post('/collections',async (ctx)=>{
    console.log(ctx)
    var obj=ctx.request.body
    let insertSql=`insert into collections(userId,videoId) values(${ctx.request.body.userId},${ctx.request.body.videoId})`
    let res=await new Promise((resolve,reject)=>{
        db.query(insertSql,(err,result)=>{
            if(err){
                throw err
            }
            else{
                resolve('add success')
            }
        })
    })
    ctx.body=res
})

// http://api.lzjlw.top:3030/collect/collections?videoId=${this.videoId} && userId=${app.userId}
collect.delete('/collections',async (ctx)=>{
    var obj=ctx.query
    console.log(obj)
    let deleteSql=`delete from collections where videoId=${obj.videoId} and userId=${obj.userId}`
    let res=await new Promise((resolve,reject)=>{
        db.query(deleteSql,(err,result)=>{
            if(err){
                throw err
            }
            else{
                resolve('delete success')
            }
        })
    })
    ctx.body=res

})

module.exports=collect